Skip to content

Make email revoking URL harder to guess. - #491

Merged
Peterburnett merged 1 commit into
TOTARA_20_STABLEfrom
harden-email-revoke
Dec 1, 2026
Merged

Peterburnett merged 1 commit into
TOTARA_20_STABLEfrom
harden-email-revoke

Conversation

@jaypha

@jaypha jaypha commented Nov 25, 2026 •

Copy link
Copy Markdown

Make the factor revoke email a little harder to guess by adding the secret to it.

Previously the "this wasn't me" revoke link only needed the
tool_mfa instance ID. IDs are sequential, so anyone could revoke a
user's email factor and end all of their sessions without
authenticating.

Revoke link (factor/email/email.php, renderer.php):

  • Include the emailed code in the revoke link and require it
    (secret is now a required param).
  • Limit guesses on the unauthenticated revoke form.
  • Reject expired codes before an attempt is counted.
  • Check the user exists and isn't deleted before revoking.
  • Suspend accounts with user_suspend_user() instead of a raw
    set_field, so user_updated/user_suspended events fire

Email-link sign-in (pass=1):

  • Only accept pending, unrevoked email codes.
  • Reject expired codes.
  • Load the lock state from the DB before checking for the locked
    state, so a fresh session can't get around the lockout.

Both:

  • Compare codes with hash_equals() on strings (constant time, no
    type juggling).

Other:

  • Add a test that the revoke link in the email includes the code.
  • Version bumps.

@jaypha
jaypha force-pushed the harden-email-revoke branch 2 times, most recently from 63d6f3f to 5ec7f4e Compare November 26, 2026 01:14
@TomoTsuyuki
TomoTsuyuki requested review from TomoTsuyuki and a balanced review from Copilot November 26, 2026 05:51

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The unauthenticated revocation endpoint allows a secret=0 bypass and unlimited six-digit code guesses, enabling attackers to revoke email MFA and terminate users’ sessions.

3 open issues
What changed in this PR

This PR aims to make email-factor revocation links harder to guess by requiring the verification secret alongside the factor record ID.

Changes:

  • Adds the secret to revocation links and checks it before revoking email factors.
  • Adds a renderer test confirming that revocation links include the secret.
  • Increments the main plugin and email-factor versions.
File Description
version.php Increments the plugin version and release number.
factor/​email/​version.php Increments the email-factor version.
factor/​email/​tests/​factor_test.php Adds coverage for the secret in rendered revocation links.
factor/​email/​renderer.php Includes the verification secret in revocation URLs.
factor/​email/​email.php Requires and checks the secret; removes an unused assignment.

Review effort: Balanced

Comment thread factor/email/email.php Outdated
redirect(new moodle_url('/'));
} else if ($fromform = $form->get_data()) {
if (empty($instance)) {
if (empty($instance) || ($instance->secret != $secret)) {
Comment thread factor/email/email.php Outdated
Comment on lines 75 to 76
if (empty($instance) || ($instance->secret != $secret)) {
$message = get_string('error:badcode', 'factor_email');
Comment thread factor/email/tests/factor_test.php Resolved
@TomoTsuyuki

Copy link
Copy Markdown
Contributor

How long should the revocation link remain valid? If it’s meant to expire with the sign-in code, could we enforce that in the revocation handler too?

The new test checks that the email contains the link. Could we also test the handler itself: a valid link should revoke email MFA, while an invalid or expired link should leave both the user’s email MFA and their sessions unchanged?

Previously the "this wasn't me" revoke link only needed the
tool_mfa instance ID. IDs are sequential, so anyone could revoke a
user's email factor and end all of their sessions without
authenticating.

Revoke link (factor/email/email.php, renderer.php):
- Include the emailed code in the revoke link and require it
  (secret is now a required param).
- Limit guesses on the unauthenticated revoke form.
- Reject expired codes before an attempt is counted.
- Check the user exists and isn't deleted before revoking.
- Suspend accounts with user_suspend_user() instead of a raw
  set_field, so user_updated/user_suspended events fire

Email-link sign-in (pass=1):
- Only accept pending, unrevoked email codes.
- Reject expired codes.
- Load the lock state from the DB before checking for the locked
  state, so a fresh session can't get around the lockout.

Both:
- Compare codes with hash_equals() on strings (constant time, no
  type juggling).

Other:
- Add a test that the revoke link in the email includes the code.
- Version bumps.
@jaypha
jaypha force-pushed the harden-email-revoke branch from 5ec7f4e to dbf03b7 Compare December 1, 2026 02:16
@Peterburnett
Peterburnett merged commit 1debd7a into TOTARA_20_STABLE Dec 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants